/*
 * author: VDaras
 */

#include "Camera.h"
#include "physics/EntityContactInfo.h"

Camera::Camera(int x, int y, int width, int height)
{
    //    m_previousFocus = Math::Vector2I(0, 0);

    //setup Rect
    m_clip.Set( y, x, height, width );

    m_xBorder = width;
    m_yBorder = height;
}

Camera::Camera(const Camera& c)
{
    //    m_previousFocus = Math::Vector2I(0, 0);
    m_clip = c.m_clip;

    m_xBorder = c.m_xBorder;
    m_yBorder = c.m_yBorder;
}

Camera::~Camera()
{
}

/*
 * Focuses camera on x,y
 */

void Camera::SetFocus(int x, int y)
{
    //    m_previousFocus = m_clip.GetOrigin();
    m_clip.SetTop( y );
    m_clip.SetLeft( x );

    if( m_clip.GetLeft( ) < 0 )
    {
        m_clip.SetLeft( 0 );
    }
    if( m_clip.GetTotalWidth( ) > m_xBorder )
    {
        m_clip.SetLeft( m_xBorder - m_clip.GetWidth( ) );
    }
    if( m_clip.GetTop( ) < 0 )
    {
        m_clip.SetTop( 0 );
    }
    if( m_clip.GetTotalHeight( ) > m_yBorder )
    {
        m_clip.SetTop( m_yBorder - m_clip.GetHeight( ) );
    }
}

/*
 * Set's maximum camera edges. camera.x + camera.width must always be below maxX.
 * The same applies for maxY.
 */

void Camera::SetBorders(int maxX, int maxY)
{
    m_xBorder = maxX;
    m_yBorder = maxY;
}

/*
 * Moves camera
 */

void Camera::Move(int x, int y) //moves camera , if it hits a border , it retains it's position
{
    //    m_previousFocus = m_clip.GetOrigin();

    m_clip.SetTop( m_clip.GetTop( ) + y );
    m_clip.SetLeft( m_clip.GetLeft( ) + x );

    if( m_clip.GetLeft( ) < 0 )
    {
        m_clip.SetLeft( 0 );
    }
    if( m_clip.GetTotalWidth( ) > m_xBorder )
    {
        m_clip.SetLeft( m_xBorder - m_clip.GetWidth( ) );
    }
    if( m_clip.GetTop( ) < 0 )
    {
        m_clip.SetTop( 0 );
    }
    if( m_clip.GetTotalHeight( ) > m_yBorder )
    {
        m_clip.SetTop( m_yBorder - m_clip.GetHeight( ) );
    }
}

const Math::Vector2F Camera::GetFocus()
{
    return m_clip.GetOrigin( ).ConvertTo<float>();
}

const Math::Rect *Camera::GetRect() const
{
    return &m_clip;
}
